package com.sunday.common.mq.rocket.brave;

import brave.Span.Kind;
import brave.messaging.ProducerRequest;
import brave.propagation.Propagation.RemoteGetter;
import brave.propagation.Propagation.RemoteSetter;
import org.apache.rocketmq.common.message.Message;
import org.springframework.messaging.support.MessageHeaderAccessor;

import java.util.Map;

/**
 * 消息生产者请求
 *
 * @author zsy
 * @see brave.spring.rabbit.MessageProducerRequest
 * @see brave.spring.rabbit.SpringRabbitTracing#SpringRabbitTracing(brave.spring.rabbit.SpringRabbitTracing.Builder)
 * @see brave.spring.rabbit.TracingMessagePostProcessor#postProcessMessage(org.springframework.amqp.core.Message)
 * @since 2023/7/31
 */
final class MessageProducerRequest extends ProducerRequest {
    static final RemoteGetter<MessageProducerRequest> GETTER =
            new RemoteGetter<>() {
                @Override
                public Kind spanKind() {
                    return Kind.PRODUCER;
                }

                @Override
                public String get(MessageProducerRequest request, String name) {
                    return MessageHeaders.getHeaderIfString(request.delegate, name);
                }

                @Override
                public String toString() {
                    return "properties::get";
                }
            };

    static final RemoteSetter<MessageProducerRequest> SETTER =
            new RemoteSetter<>() {
                @Override
                public Kind spanKind() {
                    return Kind.PRODUCER;
                }

                @Override
                public void put(MessageProducerRequest request, String name, String value) {
                    MessageHeaders.setHeader(request.delegate, name, value);
                }

                @Override
                public String toString() {
                    return "properties::put";
                }
            };

    final Message delegate;

    MessageProducerRequest(Message delegate) {
        if (delegate == null) throw new NullPointerException("delegate == null");
        this.delegate = delegate;
    }

    @Override
    public Kind spanKind() {
        return Kind.PRODUCER;
    }

    @Override
    public Object unwrap() {
        return delegate;
    }

    @Override
    public String operation() {
        return "send";
    }

    @Override
    public String channelKind() {
        return "queue";
    }

    @Override
    public String channelName() {
        String topic = delegate.getTopic();
        return topic != null ? topic : null;
    }

    @Override
    public String messageId() {
        Map<String, String> properties = delegate.getProperties();
        /** {@link org.apache.rocketmq.common.message.MessageClientIDSetter#setUniqID(org.apache.rocketmq.common.message.Message)} */
//        return properties != null ? properties.get(MessageConst.PROPERTY_UNIQ_CLIENT_MESSAGE_ID_KEYIDX) : null;
        /** {@link MessageHeaderAccessor.MutableMessageHeaders#setImmutable()} */
        return properties != null ? properties.get(org.springframework.messaging.MessageHeaders.ID) : null;
    }

}
